package org.hiberlyzer.example;

import org.hiberlyzer.DataSourceFactory;
import org.hiberlyzer.impl.DataSourceFactoryImpl;
import org.hiberlyzer.ui.ApplicationWindow;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.HSQLDialect;
import org.hsqldb.jdbcDriver;

public class ExampleApp
{
	public static void main(String[] args)
	{
		Configuration config = new Configuration()
			.setProperty(Environment.DIALECT, HSQLDialect.class.getName())
			.setProperty(Environment.DRIVER, jdbcDriver.class.getName())
			.setProperty(Environment.URL, "jdbc:hsqldb:mem:test")
			.setProperty(Environment.USER, "sa")
			.setProperty(Environment.PASS, "")
			.setProperty(Environment.HBM2DDL_AUTO, "create")
			.addClass(Artist.class)
			.addClass(Album.class)
			.addClass(Track.class);
		SessionFactory sessionFactory = config.buildSessionFactory();
		DataSourceFactory dsFactory = new DataSourceFactoryImpl(sessionFactory);
		loadExampleData(sessionFactory);
		
		new ApplicationWindow(dsFactory).setVisible(true);
	}

	private static void loadExampleData(SessionFactory sessionFactory)
	{
		Session session = sessionFactory.openSession();
		new SampleData().saveData(session);
		session.flush();
		session.close();
	}
}
